/*
 * ECARX Technology Limited is the owner of the copyright and the trade secret of this software.
 * Without permission, no one has the right to obtain, disclose or use this software in any way.
 */
package com.ecarx.systemui.plugin.ui.statusbar

import android.content.Context
import android.util.AttributeSet
import android.view.View
import androidx.core.content.res.ResourcesCompat
import com.ecarx.systemui.plugin.R
import com.ecarx.systemui.plugin.controller.VoiceController
import com.ecarx.systemui.plugin.ui.custom.SystemBarButton
import com.ecarx.systemui.plugin.utils.LogUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.lang.Exception

class VoiceChatView : SystemBarButton, VoiceController.VrStatusListener {

    companion object {
        private const val TAG = "VoiceChatView"
    }

    private var mVoiceController:VoiceController? = null

    constructor(context: Context?) : this(context!!, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(context!!, attrs, 0) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context!!,
        attrs,
        defStyleAttr
    ) {

    }

    override fun getContentDescriptionString(): String {
        return "VoiceChatView"
    }

    override fun initView() {
        super.initView()
        LogUtils.d(TAG, "initView() called")
        setImageDrawable(
            ResourcesCompat
                .getDrawable(
                    resources,
                    R.drawable.ic_voice,
                    context.theme
                )
        )
        visibility =  View.GONE
        setOnClickListener { v: View? ->
            mVoiceController?.sessionStop()
        }
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        registerVoiceStatus()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        unregisterVoiceStatus()
    }

    private fun registerVoiceStatus() {
        LogUtils.d(TAG, "registerVoiceStatus() called")
        CoroutineScope(Dispatchers.IO).launch {
            try {
                val voiceController = VoiceController(context)
                voiceController.init(this@VoiceChatView)
                mVoiceController = voiceController
                LogUtils.d(TAG, "registerVoiceStatus: ")
            } catch (exception: ClassNotFoundException) {
                exception.printStackTrace()
            } catch (throwable: NoClassDefFoundError) {
                throwable.printStackTrace()
            } catch (e: Exception){
                e.printStackTrace()
            }
        }
    }

    override fun onVrState(state: Int, info: String?) {
        LogUtils.d(TAG, "onVrState() called with: state = $state, info = $info")
        showIcon(state != 0)
    }

    private fun unregisterVoiceStatus() {
        mVoiceController?.release()
    }

    private fun showIcon(show: Boolean){
        LogUtils.d(TAG, "showIcon() called with: show = $show")
        CoroutineScope(Dispatchers.Main).launch {
            visibility = if(show){
                View.VISIBLE
            } else {
                View.GONE
            }
        }
    }
}